﻿using System;
using System.Web;
using System.Web.UI;
using System.Collections.Generic;
using System.Collections;
using System.Web.Globalization;
using System.Collections.Specialized;
using System.Reflection;
using System.Configuration;
using System.Text;
using System.Web.UI.HtmlControls;

using Developmentalmadness.Web.UI.AjaxAbstractions;

[assembly: WebResource("Developmentalmadness.Web.UI.ExtJsControlLibrary.Scripts.adapter.ext.ext-base.js", "application/x-javascript")]
[assembly: WebResource("Developmentalmadness.Web.UI.ExtJsControlLibrary.Scripts.ext-all-debug.js", "application/x-javascript")]
[assembly: WebResource("Developmentalmadness.Web.UI.ExtJsControlLibrary.Scripts.ext-all.js", "application/x-javascript")]
[assembly: WebResource("Developmentalmadness.Web.UI.ExtJsControlLibrary.Scripts.resources.css.ext-all.css", "text/css", PerformSubstitution = true)]

//for simplicity, use ext-all.js instead of ext-core.js which requires us to manage the script files for every component

//[assembly: WebResource("Developmentalmadness.Web.UI.ExtJsControlLibrary.Scripts.ext-core-debug.js", "application/x-javascript")]
//[assembly: WebResource("Developmentalmadness.Web.UI.ExtJsControlLibrary.Scripts.ext-core.js", "application/x-javascript")]
namespace Developmentalmadness.Web.UI.ExtJsControlLibrary
{
    /// <summary>
    /// Acts as ScriptManager for pages without &lt;form runat="server"&gt; element or pages
    /// which will not be using native ASP.NET AJAX scripts.
    /// </summary>
    public class ExtJsScriptManager : ScriptManagerBase
    {
        private List<ScriptDescriptor> _descriptors = new List<ScriptDescriptor>();

        public ExtJsScriptManager()
        {

        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            if (!base.DesignMode)
            {
                Page.InitComplete += new EventHandler(OnPageInitComplete);
                Page.PreRenderComplete += new EventHandler(OnPagePreRenderComplete);
            }
        }

        void OnPageInitComplete(object sender, EventArgs e)
        {
            if (base.DesignMode)
                return;

            RegisterFrameworkStyles(base.ClientScript);
        }

        private void OnPagePreRenderComplete(object sender, EventArgs e)
        {
            //add framework base dependencies
            AddFrameworkScripts();

            // add descriptor scripts
            StringBuilder script = new StringBuilder();

            // begin startup script registration
            script.Append("Ext.onReady(function(){");

#if DEBUG
            //for readability
            script.AppendLine();
#endif

            foreach (ScriptDescriptor desc in _descriptors)
            {
                // get the client-side initialization script
                script.AppendLine(((ScriptControlDescriptorBase)desc).WriteScript());
            }

            // close Ext.onReady()
            script.Append("});");

#if DEBUG
            //for readability
            script.AppendLine();
#endif

            base.ClientScript.RegisterStartupScript(this.GetType(),
                this.ClientID + "PageScript",
                script.ToString(), true);
        }

        /// <summary>
        /// Called by IScriptControl objects to register client-side object definitions
        /// with the startup script.
        /// </summary>
        /// <param name="scriptControl">The IScriptControl to be registered</param>
        public override void RegisterScriptDescriptors(IScriptControl scriptControl)
        {
            // get the list of property descriptors from the control
            IEnumerable<ScriptDescriptor> descriptors = scriptControl.GetScriptDescriptors();

            // add property descriptors to global (page) list
            foreach (ScriptDescriptor d in descriptors)
                _descriptors.Add(d);
        }



        /// <summary>
        /// Registers ExtJs framework scripts with the page
        /// </summary>
        private void AddFrameworkScripts()
        {
            // add script framework script dependencies (base scripts only, 
            // control depencies should be added by IScriptControl)
            if (Page != null)
            {
                // get the list of framework scripts
                IList<ScriptReference> references = GetFrameworkScripts(ClientScript);

                // register each script reference returned
                RegisterScriptReferences(references);
            }
        }

        /// <summary>
        /// Registers ExtJs css files with the page
        /// </summary>
        internal static void RegisterFrameworkStyles(ClientScriptManagerBase ClientScript)
        {
            //TODO: using ext-all for convenience, but should use specific scripts/css eventually
            String url = ClientScript.GetWebResourceUrl(typeof(ExtJsPanel),
                "Developmentalmadness.Web.UI.ExtJsControlLibrary.Scripts.resources.css.ext-all.css");

            ClientScript.RegisterClientCssInclude(typeof(ExtJsPanel), "_extallcss", url);
        }

        /// <summary>
        /// Gets ExtJs framework js files for registration with the page
        /// </summary>
        internal static List<ScriptReference> GetFrameworkScripts(ClientScriptManagerBase ClientScript)
        {
            List<ScriptReference> references = new List<ScriptReference>();

            references.Add(new ScriptReference(ClientScript.GetWebResourceUrl(typeof(ExtJsScriptManager),
                "Developmentalmadness.Web.UI.ExtJsControlLibrary.Scripts.adapter.ext.ext-base.js")));
#if DEBUG
            references.Add(new ScriptReference(ClientScript.GetWebResourceUrl(typeof(ExtJsScriptManager),
                "Developmentalmadness.Web.UI.ExtJsControlLibrary.Scripts.ext-all-debug.js")));
#endif
#if !DEBUG
                references.Add(new ScriptReference(ClientScript.GetWebResourceUrl(typeof(ExtJsScriptManager),
                    "Developmentalmadness.Web.UI.ExtJsControlLibrary.Scripts.ext-all.js")));
#endif

            return references;
        }
    }
}
